package com.hsnn.medstgmini.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.DicCompType;
import com.hsnn.medstgmini.base.dict.model.DicDrugspec;
import com.hsnn.medstgmini.base.dict.service.DicCompTypeManager;
import com.hsnn.medstgmini.base.dict.service.DicDrugspecManager;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompgroup;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.service.StdCompgroupManager;
import com.hsnn.medstgmini.base.std.service.StdCompinfoManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.service.DynamicSelectManager;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetail;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeDetailManager;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.SelectForm;

/**
 * 联动查询
 * 
 * @author qiulei
 *
 */

@Controller
@RequestMapping("/selectController")
public class DynamicSelectController {

	// private SelectManager selectManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private DynamicSelectManager selectManager;

	@Autowired
	private StdCompgroupManager stdCompgroupManager;

	@Autowired
	private DicCompTypeManager dicCompTypeManager;

	@Autowired
	private BidProjectManager bidProjectManager;

	@Autowired
	private DicDrugspecManager dicDrugspecManager;

	@Autowired
	private StdCompinfoManager stdCompinfoManager;

	@Autowired
	private BidSubscribeDetailManager bidSubscribeDetailManager;
	
	@Autowired
	private BidSubscribeManager bidSubscribeManager;
	
	
	@RequestMapping(value = "/getOrderStatus")
	@ResponseBody
	public List<SelectForm> getOrderStatus() {
		String classifyCode = request.getParameter("classifyCode");
		List<SelectForm> list = selectManager.getOrderStatus(classifyCode);
		return list;
	}
	
	@RequestMapping(value = "/getArchiveType")
	@ResponseBody
	public List<SelectForm> getArchiveType() {
		String classifyCode = request.getParameter("classifyCode");
		List<SelectForm> list = selectManager.getArchiveType(classifyCode);
		return list;
	}
	
	
	@RequestMapping(value = "/getArea")
	@ResponseBody
	public List<SelectForm> getArea() {
		String fatherId = request.getParameter("ID");
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "000000";
		}
		List<SelectForm> list = selectManager.getArea(fatherId);
		return list;
	}
	
	@RequestMapping(value = "/getExpertType")
	@ResponseBody
	public List<SelectForm> getExpertType() {
		String fatherId = request.getParameter("ID");
		String statusTag = request.getParameter("status");
		int status =0;
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "0";
		}
		if(StringUtils.isNotBlank(statusTag)){
			status=Constants.ENABLED;
		}
		List<SelectForm> list = selectManager.getDicExpertType(fatherId,status);
		return list;
	}
	
	@RequestMapping(value = "/getQYArea")
	@ResponseBody
	public List<SelectForm> getQYArea() {
		String fatherId = request.getParameter("ID");
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "000000";
		}
		List<SelectForm> list = selectManager.getQYArea(fatherId);
		return list;
	}

	@RequestMapping(value = "/getPhar")
	@ResponseBody
	public List<SelectForm> getPhar() {
		String fatherId = request.getParameter("ID");
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "0";
		}
		List<SelectForm> list = selectManager.getPhar(fatherId);
		return list;
	}

	@RequestMapping(value = "/getForm")
	@ResponseBody
	public List<SelectForm> getForm() {
		String fatherId = request.getParameter("ID");
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "0";
		}
		List<SelectForm> list = selectManager.getForm(fatherId);
		return list;
	}

	@RequestMapping(value = "/getMaterial")
	@ResponseBody
	public List<SelectForm> getMaterial() {
		String fatherId = request.getParameter("ID");
		if (null == fatherId || "".equals(fatherId)) {
			fatherId = "0";
		}
		List<SelectForm> list = selectManager.getMaterial(fatherId);
		return list;
	}

	@RequestMapping(value = "/getHospType")
	@ResponseBody
	public List<SelectForm> getHospType(String ID) {
		if (StringUtils.isBlank(ID)) {
			ID = null;
		}
		return selectManager.getHospType(ID);
	}
	
	@RequestMapping(value = "/getQYHospType")
	@ResponseBody
	public List<SelectForm> getQYHospType(String ID) {
		if (StringUtils.isBlank(ID)) {
			ID = null;
		}
		return selectManager.getQYHospType(ID);
	}
	
	/**
	 * 
	 * @category 根据自定义字典类型编码查询
	 * @author 邱磊
	 * @date 2015年6月10日 下午7:06:29
	 * @param ID
	 *            ：自定义字典类型编码
	 * @return
	 */
	@RequestMapping(value = "/getDicSys")
	@ResponseBody
	public List<SelectForm> getDiSys(String ID) {
		if (null != ID && !"".equals(ID.trim())) {
			return selectManager.getDiSys(ID);
		}
		return null;
	}

	@RequestMapping("/getProject")
	@ResponseBody
	public List<SelectForm> getProject(String ID) {
		if (StringUtils.isBlank(ID)) {
			ID = null;
		}
		return selectManager.getProject(ID);
	}
	/**
	 * 
	 *@category 获取当前医疗机构有效项目
	 *@author 邱磊
	 *@date 2015年6月29日 下午1:40:36
	 *@param ID
	 *@return
	 */
	@RequestMapping("/getMyProj")
	@ResponseBody
	public List<SelectForm> getMyProj(String ID){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String hospCode = ((HospInfo )user.getInfo()).getHospCode();
		return selectManager.getMyProj(hospCode);
	}
	/**
	 * 
	 *@category 获取当前生产企业有效项目
	 *@author 邱磊
	 *@date 2015年6月29日 下午1:40:36
	 *@param ID
	 *@return
	 */
	@RequestMapping("/getCompProj")
	@ResponseBody
	public List<SelectForm> getCompProj(String ID){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String compId = ((StdCompinfo )user.getInfo()).getCompId();
		return selectManager.getCompProj(compId);

	}
	
	/**
	 * 
	 *@category 获取当前生产企业有效项目
	 *@author wangbing
	 *@date 2015年6月29日 下午1:40:36
	 *@param 
	 *@return
	 */
	@RequestMapping("/getProjByProdCompId")
	@ResponseBody
	public List<SelectForm> getProjByProdCompId(){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String compCode = ((StdCompinfo )user.getInfo()).getCompCode();
		return selectManager.getProjByProdCompId(compCode);

	}
	
	/**
	 * 
	 *@category 获取所有有效项目
	 *@author 邱磊
	 *@date 2015年6月29日 下午1:40:36
	 *@param ID
	 *@return
	 */
	@RequestMapping("/getProj")
	@ResponseBody
	public List<SelectForm> getProj(String ID){
		return selectManager.getProj();
	}
	/**
	 * 
	 * @category 获取集团库信息
	 * @author 韩守松
	 * @date 2015年6月12日
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getStdCompGroup", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getStdCompgroup() {
		StdCompgroup stdCompgroup = new StdCompgroup();
		String compGroupId = request.getParameter("compGroupId");
		List<StdCompgroup> list = new ArrayList<StdCompgroup>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();

		if (StringUtils.isEmpty(compGroupId)) {
			list = stdCompgroupManager.getAll();
		} else {
			stdCompgroup = stdCompgroupManager.getById(compGroupId);
			SelectForm form = new SelectForm();
			form.setValue(stdCompgroup.getCompGroupId());
			form.setText(stdCompgroup.getCompGroupName());
			selectFormlist.add(form);
		}

		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				stdCompgroup = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(stdCompgroup.getCompGroupId());
				form.setText(stdCompgroup.getCompGroupName());
				selectFormlist.add(form);
			}
		}

		return selectFormlist;
	}
	
	/**
	 * 
	 * @category 获取启用的集团库信息
	 * @author 韩守松
	 * @date 2015年6月12日
	 * @param
	 * @return
	 */
	@RequestMapping("/getQYCompGroup")
	@ResponseBody
	public List<SelectForm> getQYCompgroup() {
		StdCompgroup stdCompgroup = new StdCompgroup();
		List<StdCompgroup> list = new ArrayList<StdCompgroup>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		list = stdCompgroupManager.getQYCompgroup();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				stdCompgroup = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(stdCompgroup.getCompGroupId());
				form.setText(stdCompgroup.getCompGroupName());
				selectFormlist.add(form);
			}
		}

		return selectFormlist;
	}

	/**
	 * 
	 * @category 获取企业注册类型
	 * @author 韩守松
	 * @date 2015年6月12日
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getDicCompType", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getDicCompType() {
		DicCompType dicCompType = new DicCompType();
		String compRegId = request.getParameter("compRegId");
		List<DicCompType> list = new ArrayList<DicCompType>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();

		if (StringUtils.isEmpty(compRegId)) {
			list = dicCompTypeManager.getDicCompTypeListByFatherId("0");
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicCompType = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicCompType.getCompRegId());
					form.setText(dicCompType.getCompRegName());
					selectFormlist.add(form);
				}
			}
		} else {
			list = dicCompTypeManager.getDicCompTypeListByFatherId(compRegId);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicCompType = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicCompType.getCompRegId());
					form.setText(dicCompType.getCompRegName());
					selectFormlist.add(form);
				}
			}
		}
		return selectFormlist;
	}
	
	/**
	 * 
	 * @category 获取启用的企业注册类型
	 * @author 韩守松
	 * @date 2015年6月12日
	 * @param
	 * @return
	 */
	@RequestMapping("/getQYCompType")
	@ResponseBody
	public List<SelectForm> getQYCompType() {
		DicCompType dicCompType = new DicCompType();
		String compRegId = request.getParameter("compRegId");
		List<DicCompType> list = new ArrayList<DicCompType>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();

		list = dicCompTypeManager.getQYCompTypeListByFatherId(compRegId);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				dicCompType = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(dicCompType.getCompRegId());
				form.setText(dicCompType.getCompRegName());
				selectFormlist.add(form);
			}
		}
		return selectFormlist;
	}

	/**
	 * @category 获取项目下拉列表数据
	 * @author 韩守松
	 * @date 2015年6月16日
	 * @param @return
	 */
	@RequestMapping(value = "/getDicBidCompany", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getDicBidCompany(Pagination page) {
		BidProject bidProject = new BidProject();
		String projId = request.getParameter("projId");
		List<BidProject> list = new ArrayList<BidProject>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();

		if (StringUtils.isEmpty(projId)) {
			list = bidProjectManager.getBidProjectSelectList(page, bidProject);
		}
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				bidProject = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(bidProject.getProjId());
				form.setText(bidProject.getName());
				selectFormlist.add(form);
			}
		}

		return selectFormlist;
	}

	/**
	 * @category 获取规格父级id
	 * @author 韩守松
	 * @date 2015年6月24日
	 * @param @param page
	 * @param @return
	 */
	@RequestMapping(value = "/getDicDrugspec", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getDicDrugspecFatherId(Pagination page,
			DicDrugspec dicDrugspec) {
		String fatherIdStr = request.getParameter("fatherIdStr");
		List<DicDrugspec> list = new ArrayList<DicDrugspec>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		if (StringUtils.isEmpty(fatherIdStr) || "0".equals(fatherIdStr)) {
			list = dicDrugspecManager.getDicDrugspecListByFatherId(fatherIdStr);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicDrugspec = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicDrugspec.getSpecId());
					form.setText(dicDrugspec.getSpecCode() + " "
							+ dicDrugspec.getSpecName());
					selectFormlist.add(form);
				}
			}
		} else {
			list = dicDrugspecManager.getDicDrugspecListByFatherId(fatherIdStr);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicDrugspec = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicDrugspec.getSpecId());
					form.setText(dicDrugspec.getSpecCode() + " "
							+ dicDrugspec.getSpecName());
					selectFormlist.add(form);
				}
			}
		}

		return selectFormlist;
	}

	/**
	 * @category 获取规格父级名称
	 * @author 韩守松
	 * @date 2015年6月24日
	 * @param @param page
	 * @param @return
	 */
	@RequestMapping(value = "/getDicDrugspecName", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getDicDrugspecFatherName(Pagination page,
			DicDrugspec dicDrugspec) {
		String fatherIdStr = request.getParameter("fatherIdStr");
		List<DicDrugspec> list = new ArrayList<DicDrugspec>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		if (StringUtils.isEmpty(fatherIdStr) || "0".equals(fatherIdStr)) {
			list = dicDrugspecManager.getDicDrugspecListByFatherId(fatherIdStr);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicDrugspec = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicDrugspec.getSpecId());
					form.setText(dicDrugspec.getSpecName());
					selectFormlist.add(form);
				}
			}
		} else {
			list = dicDrugspecManager.getDicDrugspecListByFatherId(fatherIdStr);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					dicDrugspec = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(dicDrugspec.getSpecId());
					form.setText(dicDrugspec.getSpecName());
					selectFormlist.add(form);
				}
			}
		}

		return selectFormlist;
	}

	/**
	 * @category 获取生产企业列表
	 * @author 韩守松
	 * @date 2015年6月29日
	 * @param @param stdCompinfo
	 * @param @return
	 */
	@SuppressWarnings({ "unchecked", "unused" })
	@RequestMapping(value = "/getStdCompinfoCompType", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getStdCompinfoCompType(Pagination page,
			StdCompinfo stdCompinfo) {
		String CompType = request.getParameter("CompType");
		List<StdCompinfo> list = new ArrayList<StdCompinfo>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		if (StringUtils.isNotEmpty(CompType)) {
			stdCompinfo.setCompType(Short.valueOf(CompType));
			list = (List<StdCompinfo>) stdCompinfoManager.queryStdCompinfoList(page, stdCompinfo).getRows();
			/*if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					stdCompinfo = list.get(i);
					SelectForm form = new SelectForm();
					form.setValue(stdCompinfo.getCompId());
					form.setText(stdCompinfo.getCompName());
					selectFormlist.add(form);
				}
			}*/
		}
		return selectFormlist;
	}
	

	/**
	 * @category 获取预约时间下拉列表数据
	 * @author 韩守松
	 * @date   2015年9月2日
	 * @param  @param page
	 * @param  @return
	 */
	@RequestMapping(value = "/getBidSubscribeDetailTime", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getBidSubscribeDetailTime(Pagination page) {
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		String bidSubscribeId = request.getParameter("bidSubscribeId");
		List<BidSubscribeDetail> listDate = new ArrayList<BidSubscribeDetail>();
		List<BidSubscribeDetail> listTime = new ArrayList<BidSubscribeDetail>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();

		if (StringUtils.isNotBlank(bidSubscribeId)) {
			listDate = bidSubscribeDetailManager.getSubscribeDateList(bidSubscribeId);
			if(listDate.size() > 0){
					bidSubscribeDetail.setBidSubscribeId(bidSubscribeId);
					bidSubscribeDetail.setSubscribeDate(listDate.get(0).getSubscribeDate());
					listTime = bidSubscribeDetailManager.getSubscribeTimeList(bidSubscribeDetail);
			}
		}
		if (listTime.size() > 0) {
			for (int i = 0; i < listTime.size(); i++) {
				bidSubscribeDetail = listTime.get(i);
				SelectForm form = new SelectForm();
				form.setValue(bidSubscribeDetail.getSubscribeStarttimeStr()+" - "+bidSubscribeDetail.getSubscribeEndtimeStr());
				form.setText(bidSubscribeDetail.getSubscribeStarttimeStr()+" - "+bidSubscribeDetail.getSubscribeEndtimeStr());
				selectFormlist.add(form);
			}
		}

		return selectFormlist;
	}

	/**
	 * @category 获取预约日期下拉列表数据
	 * @author 韩守松
	 * @date   2015年9月2日
	 * @param  @param page
	 * @param  @return
	 */
	@RequestMapping(value = "/getBidSubscribeDetailDate", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public List<SelectForm> getBidSubscribeDetailDate(Pagination page) {
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		String subscribeId = request.getParameter("subscribeId");
		List<BidSubscribeDetail> bidSubscribeList = new ArrayList<BidSubscribeDetail>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		if (StringUtils.isNotBlank(subscribeId)) {
			bidSubscribeList = bidSubscribeDetailManager.getSubscribeDateList(subscribeId);
		}
		if ( bidSubscribeList.size() > 0) {
			for (int i = 0; i < bidSubscribeList.size(); i++) {
				bidSubscribeDetail = bidSubscribeList.get(i);
				SelectForm form = new SelectForm();
				form.setValue(bidSubscribeDetail.getSubscribeDateStr());
				form.setText(bidSubscribeDetail.getSubscribeDateStr());
				selectFormlist.add(form);
			}
		}
		return selectFormlist;
		
	}
	
	/**
	 * 
	 * @category 获取企业资质类型
	 * @author wangbing
	 * @date 2015年6月10日 下午7:06:29
	 * @return
	 */
	@RequestMapping(value = "/getProfileType")
	@ResponseBody
	public List<SelectForm> getProfileType() {
		return selectManager.getProfileType();
	}
}
